package com.demo.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.cms.dao.ArticleExtendDao;
import com.demo.cms.model.ArticleExtend;
import com.demo.cms.service.ArticleExtendService;

/**
 * 文章扩展属性服务
 * 
 * @author ryuu.kk
 *
 */
public class ArticleExtendServiceImpl implements ArticleExtendService {

	/**
	 * 文章扩展属性Dao
	 */
	private ArticleExtendDao articleExtendDao;
	
	@Override
	public Long add(ArticleExtend extention) {
		return articleExtendDao.insert(extention);
	}

	@Override
	@Transactional
	public void addAll(List<ArticleExtend> extentions) {
		if (extentions != null && extentions.size() > 0 ) {
			for (ArticleExtend e : extentions) {
				this.add(e);
			}
		}
	}
	
	@Override
	public boolean exists(Long id) {
		return articleExtendDao.exists(id);
	}
	
	@Override
	public void update(Long id, String value, String type) {
		//更新的模型
		ArticleExtend e = new ArticleExtend();
		e.setValue(value);
		e.setType(type);
		//更新条件
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		articleExtendDao.update(e, params);
	}

	@Override
	public void update(Long articleId, String title, String value, String type) {
		//更新的模型
		ArticleExtend e = new ArticleExtend();
		e.setValue(value);
		e.setType(type);
		e.setTitle(title);
		//更新条件
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		articleExtendDao.update(e, params);
	}
	
	@Override
	public void update(Long articleId, ArticleExtend articleExtend) {
		//更新的模型
		//更新条件
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		articleExtendDao.update(articleExtend, params);
	}
	
	@Override
	public void delete(Long id) {
		articleExtendDao.delete(id);
	}

	@Override
	public void deleteByKey(String key) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("key", key);
		articleExtendDao.delete(params);
	}
	
	@Override
	public void deleteAll(Long articleId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		articleExtendDao.delete(params);
	}

	@Override
	public List<ArticleExtend> findList(Long articleId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		return articleExtendDao.select(params);
	}
	
	@Override
	public ArticleExtend find(Long id) {
		return articleExtendDao.select(id);
	}
	
	@Override
	public List<ArticleExtend> findList(Long articleId, String name) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		params.put("name", name);
		List<ArticleExtend> extendList = articleExtendDao.select(params);
		
		return extendList;
	}
	
	@Override
	public List<ArticleExtend> findList(Long articleId, String name, String type) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		params.put("name", name);
		params.put("type", type);
		List<ArticleExtend> extendList = articleExtendDao.select(params);
		
		return extendList;
	}

	@Override
	public ArticleExtend find(Long articleId, String key, String type) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("articleId", articleId);
		params.put("key", key);
		params.put("type", type);
		List<ArticleExtend> extendList = articleExtendDao.select(params);
		return extendList.size() > 0 ? extendList.get(0) : null;
	}

	public void setArticleExtendDao(ArticleExtendDao articleExtendDao) {
		this.articleExtendDao = articleExtendDao;
	}
}
